package com.victor.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.victor.dao.CommentsDao;
import com.victor.dao2.UserDao;
import com.victor.entity.Comments;
import com.victor.entity.YhUsers;
import com.victor.service.CommentsService;

@Service
public class CommentsServiceImpl implements CommentsService {

	@Autowired
	private CommentsDao dao;
	@Autowired
	private UserDao userDao;

	@Override
	public boolean batchDelete(int[] ids) {
		return dao.batchDelete(ids) > 0 ? true : false;
	}

	@Override
	public boolean insertComments(Comments record) {
		return dao.insertComments(record) > 0 ? true : false;
	}

	@Override
	public Comments findById(Integer id) {
		return dao.findById(id);
	}

	@Override
	public boolean update(Comments record) {
		return dao.update(record) > 0 ? true : false;
	}

	@Override
	public List<Map<String, String>> findByPid(Integer id) {
		List<Map<String, String>> findByPid = dao.findByPid(id);
		if (findByPid != null && !findByPid.isEmpty()) {
			List<Integer> useridList = new ArrayList<>();
			findByPid.stream().forEach(c -> useridList.add(Integer.valueOf(c.get("c_userid"))));
			Integer[] arr = new Integer[useridList.size()];
			useridList.toArray(arr);
			List<YhUsers> userList = userDao.findByIds(arr);
			findByPid.stream().forEach(c -> userList.stream().forEach(d -> {
				if (c.get("c_userid").equals(String.valueOf(d.getUser_id()))) {
					c.put("nickname", d.getNickname());
					c.put("avatar", d.getAvatar());
				}
			}));
		}
		return findByPid;
	}

	@Override
	public List<Comments> findByUserIdAndPid(String cUserid, String cPid) {
		return dao.findByUserIdAndPid(cUserid, cPid);
	}

}
